﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;
using System.Web.UI.HtmlControls;

namespace WebSys.UI.Control.ZhengShou
{
    public class ProjectDZQCInfoControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        DropDownList drptype;
        CustomGridView CustomGridList1;
        Button BtnSelect, btn_Save, btn_CS;
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            InitControls();

            base.OnLoad(e);
            if (!base.Page.IsPostBack)
            {
                Bind();
            }


            foreach (GridViewRow gvr in CustomGridList1.Rows)
            {
                Button btn_dj = (Button)gvr.FindControl("btn_dj");
                btn_dj.Click += new EventHandler(btn_dj_Click);

                Button btn_jd = (Button)gvr.FindControl("btn_jd");
                btn_jd.Click += new EventHandler(btn_jd_Click);

                Button btn_bj = (Button)gvr.FindControl("btn_bj");
                btn_bj.Click += new EventHandler(btn_bj_Click);

                
            }
        }



        protected void InitControls()
        {
            drptype = (DropDownList)FindControl("drptype");
            drptype.SelectedIndexChanged += new EventHandler(drptype_SelectedIndexChanged);
            CustomGridList1 = (CustomGridView)FindControl("CustomGridList1");
            CustomGridList1.OnLoadData += new EventHandler(CustomGridList1_OnLoadData);
            BtnSelect = (Button)FindControl("BtnSelect");
            BtnSelect.Click += new EventHandler(BtnSelect_Click);
            btn_Save = (Button)FindControl("btn_Save");
            btn_Save.Click += new EventHandler(btn_Save_Click);
            btn_CS = (Button)FindControl("btn_CS");
            btn_CS.Click += new EventHandler(btn_CS_Click);
        }


        protected void Bind()
        {
            CustomGridList1.RowDataBound += new GridViewRowEventHandler(CustomGridList1_RowDataBound);
            string sqlSel = @"Select CQID,
                               HILLNO,
                               HOUSEID,
                               LOCATION,
                               HOLDERNAME,
                               REALESTATE,
                               BUILDNO,
                               STRUCTURE,
                               FLOORS,
                               BUILDDATE,
                               BUILDAREA,
                               USE,
                               REALTYPE,
                            setlimit,
                            CONFIRMED,
                            FINISHED,
                           (case when setlimit = 1 then '√' end) setlimit1,
                           (case when CONFIRMED = 1 then '√' end) CONFIRMED1,
                           (case when FINISHED = 1 then '√' end) FINISHED1
                           from CQDETAIL_TRANSFERInfo";
            DataTable dt = _EluZSDataProvider.GetTable(sqlSel);
            if (dt.Rows.Count > 0)
            {
                CustomGridList1.DataSource = dt;
                CustomGridList1.RecordCount = dt.Rows.Count;
                CustomGridList1.DataKeyNames = new string[] { "HOUSEID", "setlimit", "CONFIRMED", "FINISHED" };
                CustomGridList1.DataBind();

            }
        }
        protected void drptype_SelectedIndexChanged(object sender, EventArgs e)
        {
            Bind();
        }


        protected void CustomGridList1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            foreach (GridViewRow gvr in CustomGridList1.Rows)
            {
                if (drptype.SelectedValue == "1")
                {
                    if (CustomGridList1.DataKeys[gvr.RowIndex].Values["setlimit"].ToString() == "0")
                    {
                        Button btn_dj = (Button)gvr.FindControl("btn_dj");
                        btn_dj.Visible = true;
                        btn_dj.Enabled = true;

                    }
                    if (CustomGridList1.DataKeys[gvr.RowIndex].Values["setlimit"].ToString() == "1")
                    {
                        Button btn_dj = (Button)gvr.FindControl("btn_dj");
                        btn_dj.Visible = true;
                        btn_dj.Enabled = false;

                    }
                }
                if (drptype.SelectedValue == "2")
                {
                    if (CustomGridList1.DataKeys[gvr.RowIndex].Values["CONFIRMED"].ToString() == "0")
                    {
                        Button btn_jd = (Button)gvr.FindControl("btn_jd");
                        btn_jd.Visible = true;
                        btn_jd.Enabled = true;

                    }
                    if (CustomGridList1.DataKeys[gvr.RowIndex].Values["CONFIRMED"].ToString() == "1")
                    {
                        Button btn_jd = (Button)gvr.FindControl("btn_jd");
                        btn_jd.Visible = true;
                        btn_jd.Enabled = false;

                    }
                }
                if (drptype.SelectedValue == "3")
                {
                    if (CustomGridList1.DataKeys[gvr.RowIndex].Values["FINISHED"].ToString() == "0")
                    {
                        Button btn_bj = (Button)gvr.FindControl("btn_bj");
                        btn_bj.Visible = true;
                        btn_bj.Enabled = true;

                    }
                    if (CustomGridList1.DataKeys[gvr.RowIndex].Values["FINISHED"].ToString() == "1")
                    {
                        Button btn_bj = (Button)gvr.FindControl("btn_bj");
                        btn_bj.Visible = true;
                        btn_bj.Enabled = false;

                    }
                }
            }
        }

        protected void CustomGridList1_OnLoadData(object sender, EventArgs e)
        {
            Bind();
        }

        protected void BtnSelect_Click(object sender, EventArgs e)
        {

            string sqlwhere = string.Empty;
            string sqldel = "Delete CQDETAIL_TRANSFERInfo ";
            _EluZSDataProvider.RunSqlNon(sqldel);
            string sqlinsert = string.Format("insert into CQDETAIL_TRANSFERInfo Select t.*,sys_guid(),'' pid from ITRADE.cqdetail_transfer t");
            _EluZSDataProvider.RunSqlNon(sqlinsert);
            Bind();
        }


        /// <summary>
        /// 终结事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_bj_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            GridViewRow gvr = btn.Parent.Parent as GridViewRow;
            string ID = CustomGridList1.DataKeys[gvr.RowIndex].Values["HOUSEID"].ToString();
            string sql = "update CQDETAIL_TRANSFERInfo set FINISHED = 1 where HOUSEID= '" + ID + "'";
            _EluZSDataProvider.RunSqlNon(sql);
            Bind();
        }



        /// <summary>
        /// 决定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_jd_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            GridViewRow gvr = btn.Parent.Parent as GridViewRow;
            string ID = CustomGridList1.DataKeys[gvr.RowIndex].Values["HOUSEID"].ToString();
            string sql = "update CQDETAIL_TRANSFERInfo set CONFIRMED = 1 where HOUSEID= '" + ID + "'";
            _EluZSDataProvider.RunSqlNon(sql);
            Bind();
        }



        /// <summary>
        /// 冻结事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_dj_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            GridViewRow gvr = btn.Parent.Parent as GridViewRow;
            string ID = CustomGridList1.DataKeys[gvr.RowIndex].Values["HOUSEID"].ToString();
            string sql = "update CQDETAIL_TRANSFERInfo set setlimit = 1 where HOUSEID= '" + ID + "'";
            _EluZSDataProvider.RunSqlNon(sql);
            Bind();
        }


        /// <summary>
        /// 传送数据 更新到ITRADE.cqdetail_transfer
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
       protected void btn_CS_Click(object sender, EventArgs e)
        {
            string sqlsel = @"Select ID,
                                CQID,
                               HILLNO,
                               HOUSEID,
                               LOCATION,
                               HOLDERNAME,
                               REALESTATE,
                               BUILDNO,
                               STRUCTURE,
                               FLOORS,
                               BUILDDATE,
                               BUILDAREA,
                               USE,
                               REALTYPE,
                            setlimit,
                            CONFIRMED,
                            FINISHED from CQDETAIL_TRANSFERInfo";
            DataTable dt = _EluZSDataProvider.GetTable(sqlsel);
            if (dt.Rows.Count>0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string sql = string.Format("update ITRADE.cqdetail_transfer set setlimit='{1}',CONFIRMED='{2}',FINISHED='{3}' where HOUSEID='{0}'",dt.Rows[i]["HOUSEID"],dt.Rows[i]["setlimit"],dt.Rows[i]["CONFIRMED"],dt.Rows[i]["FINISHED"]);
                    _EluZSDataProvider.RunSqlNon(sql);
                }
                Page.ClientScript.RegisterStartupScript(this.GetType(), "s1", "<script>alert('传送数据成功！');</script>");
            }
        }

        /// <summary>
        /// 确认数据 插入或更新到zs_zsperson
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Save_Click(object sender, EventArgs e)
        {
            string sql = @"Select ID,
                                CQID,
                               HILLNO,
                               HOUSEID,
                               LOCATION,
                               HOLDERNAME,
                               REALESTATE,
                               BUILDNO,
                               STRUCTURE,
                               FLOORS,
                               BUILDDATE,
                               BUILDAREA,
                               USE,
                               REALTYPE,
                            setlimit,
                            CONFIRMED,
                            FINISHED from CQDETAIL_TRANSFERInfo";
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            if (dt.Rows.Count>0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string sql1 = "Select id from zs_zsperson where id = '"+dt.Rows[i]["Id"]+"' ";
                    DataTable dt1 = _EluZSDataProvider.GetTable(sql1);
                    if (dt1.Rows.Count>0)
                    {
                        string sql2 = string.Format("update zs_zsperson  set 丘权号='{0}', 房屋坐落='{1}',产权人='{2}',房屋结构='{3}',房屋建筑面积='{4}',房屋用途='{5}',所属层次='{6}' where id = '{7}'", dt.Rows[i]["HILLNO"], dt.Rows[i]["LOCATION"], dt.Rows[i]["HOLDERNAME"], dt.Rows[i]["STRUCTURE"], dt.Rows[i]["BUILDAREA"], dt.Rows[i]["USE"], dt.Rows[i]["FLOORS"], dt.Rows[i]["Id"]);
                        _EluZSDataProvider.RunSqlNon(sql2);
                    }
                    else
                    {
                        string sql2 = string.Format("insert into zs_zsperson (id,产权人,丘权号,房屋坐落,房屋建筑面积,房屋用途,房屋结构,所属层次) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')", dt.Rows[i]["Id"], dt.Rows[i]["HOLDERNAME"], dt.Rows[i]["HILLNO"], dt.Rows[i]["LOCATION"], dt.Rows[i]["BUILDAREA"], dt.Rows[i]["USE"], dt.Rows[i]["STRUCTURE"], dt.Rows[i]["FLOORS"]);
                        _EluZSDataProvider.RunSqlNon(sql2); 
                    }
                }

                Page.ClientScript.RegisterStartupScript(this.GetType(), "s2", "<script>alert('确认数据成功！');</script>");
            }
        }
    }
}


